package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public PageResult pageFind(SetmealPageQueryDTO dto) {
        if (null==dto){
            dto.setPage(1);
            dto.setPageSize(10);
        }
        if (dto.getPage()<0||dto.getPage()>100){
            dto.setPage(1);
        }
        if (dto.getPageSize()<0||dto.getPageSize()>100){
            dto.setPageSize(10);
        }
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<SetmealVO>page= setmealMapper.pageFind(dto);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public List<DishVO> findByCategoryId(Long categoryId) {
         return setmealMapper.findByCategoryId(categoryId);
    }

    @Override
    public void save(SetmealDTO dto) throws Exception {
        int count=setmealMapper.findByName(dto.getName());
        if (count>0){
            throw new Exception(MessageConstant.DUPLICATE_NAME);
        }
        if (dto.getCategoryId()==null) {
            throw new Exception(MessageConstant.NOT_EMPTY);
        }
        if (dto.getSetmealDishes()==null) {
            throw new Exception(MessageConstant.NOT_DISH);
        }
        dto.setStatus(0);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.save(setmeal);
        List<SetmealDish> SetmealDishlist = dto.getSetmealDishes();
        SetmealDishlist.forEach(setmealDish -> setmealDish.setSetmealId(dto.getId()));

    }

    @Override
    public void delete(List<Integer> ids) throws Exception {
        if (null==ids){
            throw new Exception(MessageConstant.NOT_SRTMEAL);
        }
         Integer count =setmealMapper.findStatusByIds(ids);
        log.info("cont={}",count);
        if (count>0){
            throw new Exception(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealDishMapper.delete(ids);
        setmealMapper.delete(ids);
    }

    @Override
    public SetmealVO selectById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO vo = new SetmealVO();
        BeanUtils.copyProperties(setmeal,vo);

        return vo;
    }

    @Override
    public void put(SetmealDTO dto) {
        setmealDishMapper.deleteById(dto.getId());
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(dto.getId()));
        log.info("接收到的参数内的集合为:{}",setmealDishes);
        setmealDishMapper.save(setmealDishes);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.put(setmeal);

    }

    @Override
    public void putStatus(Integer status, Long id) throws Exception {
        if (status == StatusConstant.ENABLE){
            List<Integer> dishIdList = setmealDishMapper.selectBySetmealId(id);
            log.info("id集合的值为：{}",dishIdList);
            Integer count = dishMapper.selectByIdAndStatus2(dishIdList);
            if (count>0) {
                throw new Exception(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }

        setmealMapper.putStatusById(status,id);

    }


}
